# -*- coding: UTF-8 -*-
'''
Created on 28/11/2014

@author: Eduardo Cardeira - Student Number: 6033

This module contains the class and methods that allow the creation of plots
based on the data extracted from the database.
'''

import matplotlib
import matplotlib.pyplot as plot
import matplotlib.patches as mpatches

from DataProcessing import DataProcessing


class GraphicsProcessing(DataProcessing):
    '''
    The GraphicsProcessing class.
    Contains methods to draw and show plots based on user input.

    Inherits:

    DataProcessing - a class containing data processing methods used to obtain
    the data for the plots.
    '''

    def __init__(self, database_name, table_name):
        '''
        The instantiation method for DataProcessing.

        Arguments:

        database_name - the name of the database to be used.
        table_name - the name of the database table the data will be selected
        from.
        '''
        DataProcessing.__init__(self, database_name, table_name)
        pass
    pass

    def plot_ipc(self, starting_year=1977, ending_year=2009):
        '''
        The plot_ipc method.
        Draws a plot of the "Indíce de Preços ao Consumidor" values for the
        given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1997 is the default starting year.
        ending_year - the year where the data interval ends.
        2009 is the default ending year.
        (There is no "Indíce de Preços ao Consumidor" data after 2009)
        '''
        plot_data = DataProcessing.get_ipc(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('I.P.C.')
        plot.title(('Evolução do Indíce de Preços ao Consumidor ao longo dos \
anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_ti(self, starting_year=1978, ending_year=2013):
        '''
        The plot_ti method.
        Draws a plot of the "Taxa de Inflação" values for the given year
        interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1978 is the default starting year.
        (There is no "Taxa de Inflação" data before 1978)
        ending_year - the year where the data interval ends.
        2013 is the default ending year.
        '''
        plot_data = DataProcessing.get_ti(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('T.I.')
        plot.title(('Evolução da Taxa de Inflação ao longo dos anos.')
                   .decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_rminmfp(self, starting_year=1977, ending_year=2012):
        '''
        The plot_rminmfp method.
        Draws a plot of the "Remuneração Miníma Mensal" values for the given
        year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2012 is the default ending year.
        (There is no "Remuneração Miníma Mensal" data after 2012)
        '''
        plot_data = DataProcessing.get_rminmfp(self, starting_year, ending_year
                                               )
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('R.Min.M.')
        plot.title(('Evolução da Remuneração Miníma Mensal dos Funcionários \
Públicos ao longo dos anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_rmaxmfp(self, starting_year=1977, ending_year=2012):
        '''
        The plot_rmaxmfp method.
        Draws a plot of the "Remuneração Máxima Mensal" values for the given
        year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2012 is the default ending year.
        (There is no "Remuneração Máxima Mensal" data after 2012)
        '''
        plot_data = DataProcessing.get_rmaxmfp(self, starting_year, ending_year
                                               )
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('R.Max.M.')
        plot.title(('Evolução da Remuneração Máxima Mensal dos Funcionários \
Públicos ao longo dos anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_evominmaxpc(self, starting_year=1978, ending_year=2012):
        '''
        The plot_evominmaxpc method.
        Draws a plot of the "Evolução das Remunerações Mínimas e Máximas
        Mensais a Preços Constantes" values for the given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1978 is the default starting year.
        (There is no "Taxa de Inflação" data before 1978)
        ending_year - the year where the data interval ends.
        2012 is the default ending year.
        (There is no "Remuneração Mensal" data after 2012)
        '''
        plot_data = DataProcessing.get_evominmaxpc(self, starting_year,
                                                   ending_year)
        x = []   # Stores x axis values.
        y1 = []  # Stores y1 axis values.
        y2 = []  # Stores y2 axis values.
        for row in plot_data:
            x.append(row[(0)])
            y1.append(row[(1)])
            y2.append(row[(2)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        fig = plot.figure()
        # Creates left and right y axis.
        ax = fig.add_subplot(111)
        ax2 = ax.twinx()
        # Creates both plots.
        ax.plot(x, y1, marker='s', linestyle='-', color='r')
        ax2.plot(x, y2, marker='s', linestyle='-', color='g')
        # Sets the plot boundaries for each side.
        # ax.axis([starting_year, ending_year, min(y1), max(y1)])
        # ax2.axis([starting_year, ending_year, min(y2), max(y2)])
        # that line was causing problems when plotting from the application!
        # Sets the plot labels for each axis.
        ax.set_yticks(y1)
        ax2.set_yticks(y2)
        ax.set_xticks(x)
        ax.set_xticklabels(ax.get_xticks(), rotation=90)
        ax.set_xlabel('Ano')
        ax.set_ylabel('R.Min.M.')
        ax2.set_ylabel('R.Max.M.')
        plot.title(('Evolução da Remuneração Miníma e Máxima Mensais dos \
Funcionários Públicos a Preços Constantes ao longo dos anos.').decode('UTF-8'))
        # Creates a legend for both colored lines.
        red_patch = mpatches.Patch(color='red', label='R.Min.M.')
        green_patch = mpatches.Patch(color='green', label='R.Max.M.')
        plot.legend(handles=[red_patch, green_patch], loc=2)
        ax.grid()
        ax2.grid()
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_pibac(self, starting_year=1977, ending_year=2013):
        '''
        The plot_pibac method.
        Draws a plot of the "Produto Interno Bruto Anual per Capita" values for
        the given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2013 is the default ending year.
        '''
        plot_data = DataProcessing.get_pibac(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('P.I.B.A.C.')
        plot.title(('Evolução do Produto Interno Bruto Anual per Capita ao \
longo dos anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_rnbac(self, starting_year=1977, ending_year=2013):
        '''
        The plot_rnbac method.
        Draws a plot of the "Rendimento Nacional Bruto Anual per Capita" values
        for the given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2013 is the default ending year.
        '''
        plot_data = DataProcessing.get_rnbac(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('R.N.B.A.C.')
        plot.title(('Evolução do Rendimento Nacional Bruto Anual per Capita ao\
 longo dos anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_rdbac(self, starting_year=1977, ending_year=2013):
        '''
        The plot_rdbac method.
        Draws a plot of the "Rendimento Disponível Bruto Anual per Capita"
        values for the given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2013 is the default ending year.
        '''
        plot_data = DataProcessing.get_rdbac(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('R.D.B.A.C.')
        plot.title(('Evolução do Rendimento Disponível Bruto Anual per Capita \
ao longo dos anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

    def plot_rca(self, starting_year=1977, ending_year=2013):
        '''
        The plot_rca method.
        Draws a plot of the "Remunerações per Capita Anuais" values for the
        given year interval.

        Arguments:

        starting_year - the year where the data interval begins.
        1977 is the default starting year.
        ending_year - the year where the data interval ends.
        2013 is the default ending year.
        '''
        plot_data = DataProcessing.get_rca(self, starting_year, ending_year)
        x = []  # Stores x axis values.
        y = []  # Stores y axis values.
        for row in plot_data:
            x.append(row[(0)])
            y.append(row[(1)])
        matplotlib.rcParams.update({'font.size': 10})  # Change label font size
        plot.plot(x, y, marker='s', linestyle='-', color='g')
        # plot.axis([starting_year, ending_year, min(y), max(y)])
        # that line was causing problems when plotting from the application!
        plot.xticks(x)
        plot.yticks(y)
        locs, labels = plot.xticks()    # These two lines allow the rotation
        plot.setp(labels, rotation=90)  # of the x axis labels.
        plot.xlabel('Ano')
        plot.ylabel('R.C.A.')
        plot.title(('Evolução das Remunerações per Capita Anuais ao longo dos \
anos.').decode('UTF-8'))
        plot.grid()
        plot.tight_layout()
        # The maximize windows code only works on windows for now!
        mng = plot.get_current_fig_manager()  # These two line maximize the
        mng.window.state('zoomed')            # plot window when shown.
        plot.show()
        pass
    pass

'''
# Test run of all the methods to verify if the values are correct.

plotter = GraphicsProcessing("Database.db", "Dados")

plotter.plot_ipc()

plotter.plot_ti()

plotter.plot_rminmfp()

plotter.plot_rmaxmfp()

plotter.plot_evominmaxpc()

plotter.plot_pibac()

plotter.plot_rnbac()

plotter.plot_rdbac()

plotter.plot_rca()
'''
